package com.qyhl.webtv.module_user.login;

import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.SpanUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;
import com.qyhl.webtv.module_user.login.code.CodeLoginFragment;
import com.qyhl.webtv.module_user.login.normal.NormalLoginFragment;
import com.qyhl.webtv.module_user.login.wx.WXLoginFragment;
import com.weavey.loading.lib.LoadingLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登陆界面
 *
 * @author helin
 * @date 2019年3月15日11:19:39
 */
@Route(path = ARouterPathConstant.LOGIN_ACTIVITY)
public class NewLoginActivity extends BaseActivity {
    private FragmentManager fragmentManager;

    /**
     * 微信登陆
     */
    private WXLoginFragment wxLoginFragment;
    /**
     * 账号密码登陆
     */
    private NormalLoginFragment normalLoginFragment;
    /**
     * 验证码登陆
     */
    private CodeLoginFragment codeLoginFragment;

    @BindView(R2.id.wxandcode)
    TextView wxandcode;
    @BindView(R2.id.userandcode)
    TextView userandcode;
    @BindView(R2.id.clause)
    TextView clause;
    @BindView(R2.id.privacy)
    TextView privacy;
    @BindView(R2.id.rule_chk)
    CheckBox ruleChk;
    @BindView(R2.id.title)
    TextView title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.login_activity_wxlogin;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar
                .statusBarDarkFont(true)
                .fullScreen(true).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        BusFactory.getBus().register(this);
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.frameLayout, wxLoginFragment = new WXLoginFragment()).commit();

    }

    @Override
    protected void setListener() {

    }

    @OnClick({R2.id.login_back, R2.id.wxandcode, R2.id.userandcode, R2.id.clause, R2.id.privacy})
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.login_back) {
            finish();
        } else if (i == R.id.wxandcode) {
            setWxAndCode(v);

        } else if (i == R.id.userandcode) {
            setUserAndCode(v);
        } else if (i == R.id.clause) {
            Bundle bundle = new Bundle();
            bundle.putString("title", "使用条款");
            bundle.putString("url", "http://vod.136.i2863.com/customerUse.html");
            bundle.putString("id", "");
            bundle.putBoolean("hasShare", false);
            RouterManager.router(ARouterPathConstant.ADV, bundle);
        } else if (i == R.id.privacy) {
            Bundle bundle = new Bundle();
            bundle.putString("title", "隐私条款");
            bundle.putString("url", CommonUtils.getInstance().getPrivacyUrl());
            bundle.putString("id", "");
            bundle.putBoolean("hasShare", false);
            RouterManager.router(ARouterPathConstant.ADV, bundle);
        }
    }

    private void setUserAndCode(View v) {
        String tag = (String) v.getTag();
        if (tag.equals("user")) {
            title.setText("账号密码登录");
            codeText(userandcode);
            if (normalLoginFragment == null) {
                normalLoginFragment = new NormalLoginFragment();
            }
            fragmentManager.beginTransaction().replace(R.id.frameLayout, normalLoginFragment).commit();
        } else if (tag.equals("code")) {
            title.setText("手机验证登录");
            userText(userandcode);
            if (codeLoginFragment == null) {
                codeLoginFragment = new CodeLoginFragment();
            }
            fragmentManager.beginTransaction().replace(R.id.frameLayout, codeLoginFragment).commit();
        }
        wxText(wxandcode);
    }

    private void setWxAndCode(View v) {
        String tag = (String) v.getTag();
        if (tag.equals("wx")) {
            title.setText("快捷登录");
            codeText(wxandcode);
            fragmentManager.beginTransaction().replace(R.id.frameLayout, wxLoginFragment).commit();
        } else if (tag.equals("code")) {
            title.setText("手机验证登录");
            wxText(wxandcode);
            if (codeLoginFragment == null) {
                codeLoginFragment = new CodeLoginFragment();
            }
            fragmentManager.beginTransaction().replace(R.id.frameLayout, codeLoginFragment).commit();
        }
        userText(userandcode);
    }

    private void wxText(TextView view) {
        view.setCompoundDrawablesWithIntrinsicBounds(null,
                ContextCompat.getDrawable(this, R.drawable.login_wechat_icon), null, null);
        view.setTag("wx");
        view.setText("微信登录");
    }

    private void userText(TextView view) {
        view.setText("账号密码登录");
        view.setTag("user");
        view.setCompoundDrawablesWithIntrinsicBounds(null,
                ContextCompat.getDrawable(this, R.drawable.login_password_icon), null, null);
    }

    private void codeText(TextView view) {
        view.setText("手机验证码登录");
        view.setTag("code");
        view.setCompoundDrawablesWithIntrinsicBounds(null,
                ContextCompat.getDrawable(this, R.drawable.login_mobilecode_icon), null, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BusFactory.getBus().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMessage(Event.finishActivity event) {
        if (event != null) {
            finish();
        }
    }

    public boolean isAgreeRule() {
        return ruleChk.isChecked();
    }
}
